package com.object;

import java.sql.SQLOutput;
import java.util.ArrayList;
import java.util.Scanner;

public class ItemCart extends Item{

    public User showItems(User user,Scanner scanner){
        for (Item itemCart : user.getItemCarts()) {
            if(itemCart.getStock()==0){
                user.getItemCarts().remove(itemCart);
                break;
            }
        }
        if(user.getItemCarts().isEmpty()){
            System.out.println("购物车为空");
            return user;
        }
        System.out.println("ID     商品名称     生产商    价格    数量     类别");
        for (Item itemCart : user.getItemCarts()) {
            System.out.println(itemCart.getId()+"   "+itemCart.getName()+"   "+itemCart.getFactory()
            +"    "+itemCart.getPrice()+"    "+itemCart.getStock()+"    "+itemCart.getType());
        }
        return user;
    }
    public User changeNum(User user,Scanner scanner,ArrayList<Item>items,String id){
        System.out.println("请输入要加几件或减几件  ");
        int num = scanner.nextInt();
        int itemNums=0,cartNums=0;
        for (Item item : items) {
            if(item.getId().equals(id)){
                itemNums = item.getStock();//获取库存
            }
        }
        for (Item itemCart : user.getItemCarts()) {
            if(itemCart.getId().equals(id)){
                cartNums = itemCart.getStock();//获取购物车数量
            }
        }
        if(num+cartNums>itemNums){
            System.out.println("该商品库存"+itemNums+"件，" +
                    "您最多可添加"+(itemNums-cartNums)+"件");
            changeNum(user,scanner,items,id);
        }else if(num<0&&Math.abs(num)>cartNums){
            System.out.println("您的购物车内有"+cartNums+"件该商品,您最多可减少"+cartNums+"件");
            changeNum(user,scanner,items,id);

        }else {
            for (Item itemCart : user.getItemCarts()) {
                if(itemCart.getId().equals(id)){
                    itemCart.setStock(itemCart.getStock()+num);
                }
                break;
            }

        }
        return user;
    }
    public void del(User user,String id){
        for (Item itemCart : user.getItemCarts()) {
            if(itemCart.getId().equals(id)){
                user.getItemCarts().remove(itemCart);
                break;
            }
        }
    }
    public boolean chooseChange(Scanner scanner){
        return scanner.next().equals("1");
    }
    //判断id在不在购物车
    public boolean idIsFind(User user,String id){
        for (Item itemCart : user.getItemCarts()) {
            if(itemCart.getId().equals(id)){
                return true;
            }
        }
        return false;
    }
    public User payFor(User user,ArrayList<Item>items,Scanner scanner){
        System.out.println("请输入需要结算的商品id");
        String id = scanner.next();
        if (idIsFind(user,id)){
            //说明有，那么直接加到购物车
            new ItemOrder().addOrder(user,scanner,id);
        }else{
            System.out.println("id不存在");
            payFor(user,items,scanner);
        }
        return user;
    }
    public boolean operate(Scanner scanner,User user,ArrayList<Item>items){
        System.out.println("1:结算   2:删除    其他----->返回");
        String choose = scanner.next();
        if(choose.equals("1")){
            System.out.println("------------结算------------");
            //将选择的id加入购物车
            payFor(user,items,scanner);
        } else if (choose.equals("2")) {
            removeId(user,scanner,items);
        }else {
            return false;
        }
        return true;
    }
    //删除购物车
    public User removeId(User user,Scanner scanner,ArrayList<Item>items){
        System.out.println("请输入您要删除商品的id---->输入1,返回");
        String id = scanner.next();
        if(id.equals("1")){
            return user;
        }
        if(new Item().idInCarts(user,id)){
            System.out.println("请选择操作 1:删除该id的所有物品   其他:修改数量");
            if(chooseChange(scanner)){
                //删除该列
                del(user,id);

            }else {
                //修改数量
                changeNum(user,scanner,items,id);
            }
        }else {
            System.out.println("您输入的id不存在，请重新输入");
            removeId(user,scanner,items);
        }
        System.out.println("是否继续修改？ 1:是   其他:否");
        if(scanner.next().equals("1")){
            removeId(user,scanner,items);
        }
        return user;
    }
    //看

}
